package org.alwayssuper.protocol.commons.transform.realtimev2025;

import org.alwayssuper.protostar.annotation.Field;
import lombok.Data;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * 2025版动力蓄电池温度数据格式 (0x08)
 * 参考AICoding报文规则文档 - 2025版新增
 * @author alwaysSuper
 */
@ToString
@Data
@Accessors(chain = true)
public class BatteryTemperatureData {

    // 动力蓄电池包个数:1~253个动力蓄电池包个数(由batteryPackTempInfoList的totalUnit获取)
    private int batteryPackCount;
    
    @Field(totalUnit = 1, desc = "动力蓄电池温度信息列表")
    private List<BatteryPackTempInfo> batteryPackTempInfoList;

    @ToString
    @Data
    @Accessors(chain = true)
    public static class BatteryPackTempInfo {
        @Field(length = 1, desc = "动力蓄电池包号:有效值范围1~50")
        private int batteryPackNumber;
        // 动力蓄电池包温度探针个数:N个温度探针,有效值范围:1~65531。“0xFF,0xFE”表示异常,“0xFF,0xFF”表示无效(由temperatureValues的totalUnit获取)
        private int temperatureProbeCount;
        
        @Field(totalUnit = 2, desc = "各温度探针检测到的温度值列表")
        private List<BatteryTemperatureValue> temperatureValues;

        // 通过temperatureValues列表长度返回temperatureProbeCount
        public int getTemperatureProbeCount() {
            return temperatureValues == null ? 0 : temperatureValues.size();
        }
    }
    
    @Data
    @Accessors(chain = true)
    public static class BatteryTemperatureValue {
        @Field(length = 1, desc = "温度探针检测到的温度值:有效值范围0~250(偏移40℃,-40℃~+210℃),精度1℃")
        private int temperatureValue;
        
        // ==================== 传输值与真实值转换方法 ====================
        
        /**
         * 获取温度值 (℃)
         * @return 温度，精度1℃，异常返回-999，无效返回-1000
         */
        public int getTemperature() {
            if (temperatureValue == 0xFE) return -999; // 异常
            if (temperatureValue == 0xFF) return -1000; // 无效
            return temperatureValue - 40; // (传输值-40) = 值(℃)，支持负值
        }
        
        /**
         * 设置温度值 (℃)
         * @param temperature 温度值，范围-40℃~+210℃
         */
        public void setTemperature(int temperature) {
            if (temperature < -40 || temperature > 210) {
                this.temperatureValue = 0xFF; // 无效值
            } else {
                this.temperatureValue = temperature + 40; // (值+40) = 传输值
            }
        }
        
        /**
         * 检查温度值是否有效
         * @return true: 有效, false: 异常或无效
         */
        public boolean isTemperatureValid() {
            return temperatureValue != 0xFE && temperatureValue != 0xFF;
        }
        
        /**
         * 获取温度等级描述
         * @return 温度等级字符串
         */
        public String getTemperatureLevelDescription() {
            if (!isTemperatureValid()) {
                return temperatureValue == 0xFE ? "异常" : "无效";
            }
            
            int realTemp = getTemperature();
            if (realTemp < -20) {
                return "极低温";
            } else if (realTemp < 0) {
                return "低温";
            } else if (realTemp < 25) {
                return "常温";
            } else if (realTemp < 45) {
                return "温热";
            } else if (realTemp < 60) {
                return "高温";
            } else {
                return "过热";
            }
        }
    }
    
    // 通过batteryPackTempInfoList列表长度返回batteryPackCount
    public int getBatteryPackCount() {
        return batteryPackTempInfoList == null ? 0 : batteryPackTempInfoList.size();
    }
}